home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / cio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  13.9 KB  |  448 lines

  1. /* 
  2.    Spectra conversion between LISE and Cessna (OTTO) Format
  3.    
  4.    Since the original FORTRAN routines were unreadable for my small brain
  5.    (approximately 100 lines of real code scatterd on about 20 files and
  6.     coverd under 1000 lines of unusable comments (name of programmer,
  7.     history of file, wether of day and bus connection to the programmers home)
  8.    I have deduced the format empirical from some real spectra.
  9.    For this reason, it may well be, that this routine does not work
  10.    well in all cases.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <spec.h>
  16.  
  17. float *spc, *err, *tim;
  18.  
  19. int   swap=FALSE;
  20. int   cessnafd=0;
  21.  
  22. help()
  23. {
  24. printf("CIO Spectra conversion between LISE and CESSNA (OTTO) Format\n");
  25. printf("Call convention:\n");
  26. printf("  cio [*.spc] [-l2o cessnafile] [-o2l cessnafile] [-swap] [-o filename]\n");
  27. printf("  options and theier meaning:\n");
  28. printf("     -l2o name  LISE -> OTTO  reads a number of files (*.spc) and\n");
  29. printf("                converts them into CESSNA format; writes to file 'name'\n");
  30. printf("     -o2l name  OTTO -> LISE  read  CESSNA 'name', extracts the spectrum\n");
  31. printf("                supplied as first parameter and generates a LISE spectrum\n");
  32. printf("     -list name list spectra in CESSNA file name\n");
  33. printf("     -swap      is for integer conversion (680x0<->VAX)\n");
  34. printf("     -o name    may be specified to redirect output of LISE spectra\n\n");
  35. }
  36.  
  37. main(argc,argv)
  38. int argc;
  39. char *argv[];
  40. {
  41. int n,m,i,j,k,max;
  42. char z[80],comment[80];
  43.  
  44.    spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  45.    err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  46.    tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  47.  
  48.    if(checkopt(argc,argv,"-swap",z)) swap=TRUE;
  49.    if(checkopt(argc,argv,"-l2o",z)) {
  50.       opencessna(z);                   /* open cessna file */
  51.       n=1;                             /* first argument (LISE file) */
  52.       while(1==1) {
  53.          strcpy(z,argv[n++]);
  54.          if(z[0]=='-') break;          /* options reached */
  55.          max=readspec(z,spc,err,tim,comment);
  56.          strupc(_spc_0nam);
  57.          cessnawrite(_spc_0nam,max,spc); /* write spectrum in CESSNA format */
  58.       }
  59.       close(cessnafd);
  60.       exit(0);
  61.    }
  62.  
  63.    if(checkopt(argc,argv,"-o2l",z)) {
  64.       cessnafd=open(z,0,0666);
  65.       if(cessnafd<0) {
  66.          strcat(z,".SPC");
  67.          cessnafd=open(z,0,0666);
  68.       }
  69.       if(cessnafd<0) {
  70.          fprintf(stderr,"cio: can not open cessna file >%s<\n",z);
  71.          exit(0);
  72.       }
  73.       strcpy(z,argv[1]);               /* get first argument = spectrum name */
  74.       max=cessnaread(z,spc);
  75.       writespec(z,spc,err,max,1,"CESSNA ");
  76.       close(cessnafd);
  77.       exit(0);
  78.    }
  79.  
  80.    if(checkopt(argc,argv,"-list",z)) {
  81.       cessnafd=open(z,0,0666);
  82.       if(cessnafd<0) {
  83.          strcat(z,".SPC");
  84.          cessnafd=open(z,0,0666);
  85.       }
  86.       if(cessnafd<0) {
  87.          fprintf(stderr,"cio: can not open cessna file >%s<\n",z);
  88.          exit(0);
  89.       }
  90.       cessnalist();
  91.       close(cessnafd);
  92.       exit(0);
  93.    }
  94.  
  95.    _help0();
  96. }
  97.  
  98. /* -----------------------------------------------------------------
  99.    Writing of data to file (descriptor=cessnafd), including swapping
  100.    ----------------------------------------------------------------- */
  101. writeb(c)
  102. unsigned char c;
  103. {
  104.    write(cessnafd,&c,1);
  105. }
  106.  
  107. writei2(i)
  108. int i;
  109. {
  110. int b1,b2;
  111. unsigned short int b;
  112.  
  113.    b=i;
  114.    if(swap) {
  115.       b1=i>>8 ; b2=i-(b1<<8);
  116.       b = (b2<<8) + b1;
  117.    }
  118.       
  119.    write(cessnafd,&b,2);
  120. }
  121.  
  122. writei4(i)
  123. int i;
  124. {
  125. unsigned long int b;
  126. int b1,b2,b3,b4;
  127.  
  128.    b=i;
  129.    if(swap) {
  130.       b4 = b & 0xff; b = b>>8;
  131.       b3 = b & 0xff; b = b>>8;
  132.       b2 = b & 0xff; b = b>>8;
  133.       b1 = b;
  134.       b = (b4 << 24) + (b3 << 16) + (b2 << 8) + b1;
  135.    }
  136.    write(cessnafd,&b,4);
  137. }
  138.  
  139. writestr(str,n)
  140. char str[];
  141. int n;
  142. {
  143. int i,j,k,l;
  144.  
  145.    l=strlen(str);
  146.    for(i=0;i<l;i++) writeb(str[i]);
  147.    while(i<n) {
  148.       writeb(' ');                  /* fill rest with blanks (stupid FORTRAN !) */
  149.       i=i+1;
  150.    }
  151. }
  152. /* -------------------------------------------------------------------
  153.    Reading of data from file (descriptor=cessnafd), including swapping
  154.    ------------------------------------------------------------------- */
  155. readb()
  156. {
  157. unsigned char c;
  158.    read(cessnafd,&c,1);
  159.    return(c);
  160. }
  161.  
  162. readi2()
  163. {
  164. int b,b1,b2;
  165. unsigned short int i;
  166.  
  167.    read(cessnafd,&i,2);
  168.    b=i;
  169.    if(swap) {
  170.       b1=i>>8 ; b2=i-(b1<<8);
  171.       b = (b2<<8) + b1;
  172.    }
  173.    return(b);      
  174. }
  175.  
  176. readi4()
  177. {
  178. unsigned long int i,b;
  179. int b1,b2,b3,b4;
  180.  
  181.    read(cessnafd,&i,4);
  182.    b=i;
  183.    if(swap) {
  184.       b4 = b & 0xff; b = b>>8;
  185.       b3 = b & 0xff; b = b>>8;
  186.       b2 = b & 0xff; b = b>>8;
  187.       b1 = b;
  188.       b = (b4 << 24) + (b3 << 16) + (b2 << 8) + b1;
  189.    }
  190.    return(b);
  191. }
  192.  
  193. readstr(str)
  194. char str[];
  195. {
  196. int i,n;
  197. char c;
  198.  
  199.    n=0;
  200.    c=0;
  201.    while(c!=32) {
  202.       c=readb();
  203.       str[n++]=c;
  204.       if(c==0) break;
  205.    }
  206.    str[n-1]=0;
  207. }
  208.  
  209. /* -------------------------------------------------------------
  210.    generate Header entry 
  211.    since there are a lot of ยง"empirical" parameters, i have used
  212.    the variablenames qm1... for these.
  213.    ------------------------------------------------------------- */
  214. header(name,qm1,qm2,qm3,qm4,fwp,bwp,dirp,hdtyp,spclen,spctyp)
  215. char name[];
  216. int qm1,qm2,qm3,qm4,fwp,bwp,dirp,hdtyp,spclen,spctyp;
  217. {
  218. int i,n;
  219. time_t izeit;
  220. struct tm *now;
  221. char zeit[80];
  222.  
  223.    izeit=time(NULL);                        /* get actual time */
  224.    now=localtime(&izeit);
  225.    strftime(zeit,18,"%H:%M %d.%m.%y",now);
  226.  
  227.    writei2(0x1501);              /* 1)  21 x I*2 words */
  228.    writei2(256);                 /* 2)  Header length is 256 words */
  229.    writei2(qm1);                 /* 3)  pointer to first free entry */
  230.    writei2(0x17);                /* 4)  type of header */
  231.    writei2(0x1a);                /* 5)  pointer to name of file */
  232.    writei2(qm2);                 /* 6)  ?? */
  233.    writei2(0x21);                /* 7)  pointer to date and time */
  234.    writei2(qm3);                 /* 8)  ?? */
  235.    writei2(qm4);                 /* 9)  ?? */
  236.    writei2(0);                   /* 10) ?? */
  237.    writei2(0);                   /* 11) ?? */
  238.    writei2(0);                   /* 12) ?? */
  239.    writei2(0);                   /* 13) ?? */
  240.    writei2(0);                   /* 14) ?? */
  241.    writei2(0);                   /* 15) ?? */
  242.    writei2(0);                   /* 16) ?? */
  243.    writei2(0);                   /* 17) ?? */
  244.    writei2(0);                   /* 18) ?? */
  245.    writei2(fwp);       /* 19) pointer to forward pointer (highly sophisticated) */
  246.    writei2(bwp);       /* 20) pointer to backward pointer (highly sophisticated) */
  247.    writei2(dirp);      /* 21) pointer to directory       */
  248.    writei2(0);                   /* 22) ??               */
  249.    writei2(0x0201);              /* 23) 2 x I*2 words    */
  250.    writei2(hdtyp);               /* 24) header type      */
  251.    writei2(0);                   /* 25) ??               */
  252.    writei2(0x0c10);              /* 26) 12 ASCII         */
  253.    writestr(name,12);            /* 27-32) file name     */
  254.    writei2(0x1210);              /* 33) 18 ASCII         */
  255.    writestr(zeit,18); /* 34-42  date and time */
  256.    writei2(0);                   /* 43) ?? */
  257.    switch(hdtyp) {
  258.    case 0x20:
  259.       writei2(0x0202);           /* 44) 2 x I*4 words */
  260.       writei4(-1);               /* 45-46) forward pointer */
  261.       writei4(-1);               /* 47-48) backward pointer */
  262.       writei2(0x0280);           /* 49)  [I*4            */
  263.       writei2(0x1080);           /* 50)   [ASCII         */
  264.       writei2(0x0c81);           /* 51)   ]12            */
  265.       writei2(0x1981);           /* 52)  ]25             */
  266.                                  /* continues with directory entries */
  267.    break;
  268.    case 0x01:
  269.       writestr("",12);           /* 44-59) spaces        */
  270.       writei2(0x0501);           /* 50)  5 x I*2 words ? */
  271.       writei2(0x0001);           /* 51)  1 ?             */
  272.       writei2(0x0001);           /* 52)  1 ?             */
  273.       writei2(spclen);           /* 53)  spectra length ? */
  274.       writei4(0);                /* 54-55)  ?             */
  275.       writeb(spctyp);           /* 56)  spectra type ?  */
  276.       writeb(spctyp);           /* 56)  spectra type ?  */
  277.       writei2((spclen*spctyp+256)/256);
  278.                                  /* 57)  length of data area ??? */
  279.       writei2(0);                /* 58) ??               */
  280.       writei2(0x0100);           /* 59) ??               */
  281.    break;
  282.    }
  283. }
  284.  
  285.  
  286.  
  287. /* ---------------------------------------------------------------
  288.    open CESSNA file.
  289.    If the file does not exist, then create a new file.
  290.    --------------------------------------------------------------- */
  291. opencessna(name)
  292. char name[];
  293. {
  294. int n,m,i,j;
  295.  
  296.    cessnafd=open(name,2,0666);
  297.    if(cessnafd<0) {
  298.       printf("creating new CESSNA file\n");
  299.       cessnafd=creat(name,0666);
  300.       header("fuckMMMBAvOE",0x35,0,0,0,0x2c,0,0x31,0x20,0,0);
  301.       close(cessnafd);
  302.       cessnafd=open(name,2,0666);
  303.    }
  304. }
  305.  
  306. /* ----------------------------------------------------------------
  307.    write spectrum in CESSNA format
  308.    ---------------------------------------------------------------- */
  309. cessnawrite(name,max,spc)
  310. char name[];
  311. int max;
  312. float spc[];
  313. {
  314. int i,n,m,j,k,l,p,a;
  315.  
  316.    lseek(cessnafd,4L,0);            /* position to pointer to next free entry */
  317.    p=readi2();                      /* read pointer to next free entry */
  318.    p=2*(p-1);                       /* construct bytepointer */
  319.    if(p==104) {                     /* if it is the first entry */
  320.       a=512;                        /* address of spectrum */
  321.    } else {
  322.       i=p-16;                       /* pointer to last entry */
  323.       lseek(cessnafd,i,0);
  324.       n=readi2();                   /* read entry */
  325.       n = 512 * (n-1);              /* construct byte pointer */
  326.       lseek(cessnafd,n+112,0);      /* read length of data area */
  327.       l=readi2();
  328.       l = 512 * l;                  /* construct offset */
  329.       a = n + l;
  330.    }
  331.    i = (a >> 9) + 1;                /* construct CESSNA pointer to address */
  332.    lseek_plus(cessnafd,p,0);             /* go to next free entry in directory */
  333.    writei2(i);                      /* write pointer to spectrum */
  334.    writeb(0);                       /* allways 0 */
  335.    writeb(0x01);                    /* type of header there */
  336.    writestr(name,12);               /* write name of spectrum */
  337.    p=p+16;                          /* point to next entry */
  338.    p = (p>>1) + 1;                  /* generate CESSNA pointer */
  339.    lseek_plus(cessnafd,4L,0);            /* position to pointer to next free entry */
  340.    writei2(p);                      /* update pointer */
  341.    
  342.    lseek_plus(cessnafd,a,0);             /* point to new spectrum entry */
  343.    header(name,0x41,0x2b,0x32,0xfe,0,0x38,0,0x01,max,2);
  344.    a= a + 506;                      /* point to data area */
  345.    lseek_plus(cessnafd,a,0);
  346.    writei2(0x8202);                 /* !!! empirical constant !!! */
  347.    writei2(max);                    /* write length of data area */
  348.    writei2(0);                      /* spare ? */
  349.    for(i=0;i<max;i++) {             /* write data */
  350.       n = spc[i];
  351.       writei4(n);
  352.    }
  353. }
  354.  
  355. /* ----------------------------------------------------------------
  356.    Read spectrum in CESSNA format
  357.    ---------------------------------------------------------------- */
  358. cessnaread(name,spc)
  359. char name[];
  360. float spc[];
  361. {
  362. int i,n,m,j,k,l,p,a;
  363. char s[80];
  364.  
  365.    lseek(cessnafd,40L,0);           /* position to pointer to directory */
  366.    p=readi2();                      /* read pointer to directory */
  367.    p = 2 * (p-1);                   /* construct bytepointer */
  368.    p = p + 8;
  369.    n=0;
  370.    while(n<25) {
  371.       lseek(cessnafd,p,0);             /* position to directory */
  372.       a=readi2();                      /* get address of spectrum */
  373.       i=readi2();                      /* skip 2 bytes */
  374.       readstr(s);                      /* read spectrum name */
  375.       if((strcmp(name,s)==0) && strlen(name)==strlen(s)) {  /* entry found */
  376.          a = 512 * (a-1);              /* byte address of spectrum */
  377.          lseek(cessnafd,a+104,0);      /* position to length entry */
  378.          l = readi2();                 /* get length of spectrum */
  379.          i = readi4();                 /* skip 4 bytes */
  380.          i = readi2();                 /* read type */
  381.          if(i!=0x0202) {               /* up to now we can only handle I*4 */
  382.             fprintf(stderr,"cio: sorry, not an I*4 spectrum\n");
  383.             close(cessnafd);
  384.             exit(0);
  385.          }
  386.          lseek(cessnafd,a+512,0);      /* go to data */
  387.          for(i=0;i<l;i++) {            /* read data */
  388.             m = readi4();
  389.             spc[i] = m;
  390.          }
  391.          return(l);   
  392.       }
  393.       n = n + 1;
  394.       p = p + 16;
  395.    }
  396.    fprintf(stderr,"cio: Spectrum >%s< not found\n",name);
  397.    close(cessnafd);
  398.    exit(0);
  399. }
  400.  
  401. /* ----------------------------------------------------------------
  402.    List names of spectra in CESSNA file
  403.    ---------------------------------------------------------------- */
  404. cessnalist()
  405. {
  406. int i,n,m,j,k,l,p,a;
  407. char s[80];
  408.  
  409.    lseek(cessnafd,40L,0);           /* position to pointer to directory */
  410.    p=readi2();                      /* read pointer to directory */
  411.    p = 2 * (p-1);                   /* construct bytepointer */
  412.    p = p + 8;
  413.    n=0;
  414.    while(n<25) {
  415.       lseek(cessnafd,p,0);             /* position to directory */
  416.       a=readi2();                      /* get address of spectrum */
  417.       i=readi2();                      /* skip 2 bytes */
  418.       readstr(s);                      /* read spectrum name */
  419.       a = 512 * (a-1);                 /* byte address of spectrum */
  420.       lseek(cessnafd,a+104,0);         /* position to length entry */
  421.       l = readi2();                    /* get length of spectrum */
  422.       i = readi4();                    /* skip 4 bytes */
  423.       i = readi2();                    /* read type */
  424.       if(strlen(s)>0) printf("%12s    %d channels     type=%4x ?\n",s,l,i);
  425.       n = n + 1;
  426.       p = p + 16;
  427.    }
  428. }
  429.  
  430. lseek_plus(fd,ptr,o)
  431. int fd,ptr,o;
  432. {
  433. int i,n,k,j;
  434.  
  435.    i=lseek(fd,ptr,o);
  436.    if(i!=ptr) {
  437.       i=lseek(fd,0L,2);
  438.       n=ptr-i;
  439.       j=0;
  440.       for(k=0;k<n;k++) write(fd,&j,1);
  441.       i=lseek(fd,ptr,o);
  442.       if(i!=ptr) printf("hier stinkts!");
  443.    }
  444.    return(i);
  445. }
  446.  
  447.  
  448.